# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=pyalpm
pkgbase="python-${_realname}"
pkgname=("python2-${_realname}" "python3-${_realname}")
pkgver=0.8.4
pkgrel=1
pkgdesc="Libalpm bindings for Python"
arch=('i686' 'x86_64')
license=('GPL')
url="https://git.archlinux.org/pyalpm.git/"
depends=("libarchive-devel")
makedepends=('python2'
             'python3'
             'gettext-devel'
             'heimdal-devel'
             'libarchive-devel'
             'libcurl-devel'
             'libgpgme-devel'
             'pacman')
source=("git+https://git.archlinux.org/pyalpm.git#commit=ec89afb96636a3449006323030dd1768c143c5f5"
        "0001-Python2-compat-Module-initialization.patch"
        "0002-Python2-compat-Use-Py_BuildValue-s-file-name.patch"
        "0003-Python2-compat-pycman-from-__future__-import-print_f.patch"
        "0004-Python2-compat-python3-python-coding-utf-8.patch"
        "0005-Use-pkg-config-add-LIBALPM_STATIC-env.-var.patch")
sha256sums=('SKIP'
            '489a3556bea9e22e809d7be31beb2dcaa74a3f776acd304d4a31cc1cfc2c547a'
            '37f510ad8b66317b68125e7b2c4d7fee8396a0e795c145c443bfb3770187bed4'
            '4b64a72f86cb6df75f37acf96de50148314ae1f44ae5ebd011b4631a7b5abcb8'
            '0faf882eda91d360a346f24591cf51d94f5f8b9ed0f8c4291e8610bb5932db98'
            '2e7ff3084cb9a4dd007a282881d5b84b8559d959a478082f7e73d1647c02c78c')

prepare() {
  cd "${srcdir}"/${_realname}
  patch -p0 -i "${srcdir}"/0001-Python2-compat-Module-initialization.patch
  patch -p0 -i "${srcdir}"/0002-Python2-compat-Use-Py_BuildValue-s-file-name.patch
  patch -p0 -i "${srcdir}"/0003-Python2-compat-pycman-from-__future__-import-print_f.patch
  patch -p0 -i "${srcdir}"/0004-Python2-compat-python3-python-coding-utf-8.patch
  patch -p0 -i "${srcdir}"/0005-Use-pkg-config-add-LIBALPM_STATIC-env.-var.patch
  
  # The git repo uses symlinks in the scripts folder to
  # the actual source files in the pycman folder. Patch
  # files created by git will therefore not contain any
  # changes to the files in scripts so copy the patched
  # ones across now.
  pushd "${srcdir}"/${_realname}
    for _file in deptest query remove sync upgrade version; do
      cp -f pycman/action_${_file}.py scripts/pycman-${_file}
    done
  popd
}

build() {
  cd "${srcdir}"
  rm -rf python{2,3}-build
  for builddir in python{2,3}-build; do
    cp -r ${_realname} ${builddir}
    pushd ${builddir} > /dev/null 2>&1
    LIBALPM_STATIC=1 \
      /usr/bin/${builddir%-build} setup.py build
    popd > /dev/null 2>&1
  done
}

package_python3-pyalpm() {
  depends=("python3")

  cd "${srcdir}/python3-build"
  /usr/bin/python3 setup.py install --prefix=/usr --root="${pkgdir}" -O1
}

package_python2-pyalpm() {
  depends=("python2")

  cd "${srcdir}/python2-build"
  /usr/bin/python2 setup.py install --prefix=/usr --root="${pkgdir}" -O1

  # Avoid conflicts.
  mv "${pkgdir}"/usr/bin/lsoptdepends "${pkgdir}"/usr/bin/lsoptdepends2
  pushd "${pkgdir}"/usr/bin > /dev/null 2>&1
    for _file in database deptest query remove sync upgrade version; do
      mv pycman-${_file} pycman2-${_file}
    done
  popd > /dev/null 2>&1
}
